package com.oa.service.impl;


import com.oa.mapper.MenuMapper;
import com.oa.pojo.Menu;
import com.oa.pojo.User;
import com.oa.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
* @author wsrwy
* @description 针对表【menu】的数据库操作Service实现
* @createDate 2023-10-22 21:08:11
*/
@Service
public class MenuServiceImpl implements MenuService{
    @Autowired
    private MenuMapper menuMapper;
    public List<Menu> selectByUserId(Integer uId){
        //一级菜单
        List<Menu> list1= menuMapper.selectByUserId(uId,0);
        for(Menu m:list1){
            int mid=m.getId();
            List<Menu> list2=menuMapper.selectByUserId(uId,mid);
            m.setList(list2);//把二级菜单保存到指定的一级菜单对象中
        }
        return list1;
    }
    @Override
    public List<Menu> selectAll(Menu menu) {
        List<Menu> menuList=menuMapper.selectAll(menu);
        return null;
    }
    @Override
    public int deleteMenu(Integer id) {
        Menu menu = new Menu();
        menu.setId(id);
        return menuMapper.deleteMenu(menu);
    }
    @Override
    public int updateMenu(Menu menu) {
        Menu menu1 = new Menu();
        List<Menu> menuList=menuMapper.selectAll(menu);
        for (int i = 0; i < menuList.size(); i++) {
            if(menuList.get(i).getMenuName().equals(menu.getMenuName()) && menu.getId()!=menuList.get(i).getId()){
                return 0;
            }
        }
        return menuMapper.updateMenu(menu);
    }
    @Override
    public int insertMenu(Menu menu) {
        Menu menu1 = new Menu();
        List<Menu> menuList=menuMapper.selectAll(menu);
        for (int i = 0; i < menuList.size(); i++) {
            if(menuList.get(i).getMenuName().equals(menu.getMenuName())){
                return 0;
            }
        }
        return menuMapper.insertMenu(menu);
    }
}




